﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace WindowsFormsApplication1.WebTest.WebTestXmlNodes
{
    [XmlRoot("FormPostHttpBody", Namespace = XmlNamespace)]
    public class FormPostHttpBodyNode : GenericWebTestNode, IXmlSerializable
    {
        public override void SetNodeProperties()
        {
            SelectedImageIndex = 4;
            ImageIndex = 4;

            Text = "Form Post Http Body";
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            Notification.BeginOperation("Processing Form Post Http Body Node", NodeOperationEventArgs.EventImportance.Detailed);

            //Read Form Post Http Body folder attributes
            ReadXmlAttributes(reader);

            //Set node properties such as Icon, Text, etc..
            SetNodeProperties();

            //Read the <FormPostHttpBody> opening tag
            reader.MoveToContent();
            bool isEmptyElement = reader.IsEmptyElement;

            reader.Read();
            reader.MoveToContent();

            bool exitLoop = false;
            while (!isEmptyElement && reader.NodeType != XmlNodeType.EndElement)
            {
                //Process sub tags
                switch (reader.Name)
                {
                    case "FileUploadParameter":
                        var fileUploadParameter = (FileUploadParameterNode)_fileUploadParameterNodeSerializer.Deserialize(reader);
                        Nodes.Add(fileUploadParameter);

                        break;
                    case "FormPostParameter":
                        var formPostParameter = (FormPostParameterNode)_formPostParameterNodeSerializer.Deserialize(reader);
                        Nodes.Add(formPostParameter);

                        break;

                    default:
                        Notification.DebugMessage("Unknown tag: " + reader.Name);
                        exitLoop = true;
                        break;
                }

                reader.MoveToContent();
                if (exitLoop) break;
            }

            //If there is closing tag read it
            if (!isEmptyElement)
                reader.ReadEndElement();

            Notification.EndOperation();
        }


        #endregion


    }

}
